<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use App\Models\Information;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class InformationController extends Controller
{
    use ModelForm;

    public $header = '资讯管理';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Information::class, function (Grid $grid) {

            $grid->model()->orderBy('created_at', 'desc');
            $grid->id('编号')->sortable();
            $grid->title('标题');
            $grid->category()->title('分类名称');
            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->like('title', '标题');
                $filter->equal('cat_id', '分类')->select(Category::selectOptions(null,'根目录', false));
                $filter->between('created_at', '创建时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Information::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->select('cat_id','分类')->options(Category::selectOptions(null,'根目录', false));
            $form->text('title', '标题')->rules('required');
            $form->text('from', '来源');
            $form->editor('content', '内容')->rules('required');

            $form->tools(function (Form\Tools $tools){
               $tools->disableView();
            });

            $form->disableViewCheck();

            $form->saving(function (Form $form){
                $form->model()->user_id = Admin::user()->id;

                $image = '';
                $content = $form->content;
                if ($content) {
                    $preg = "/(href|src|url)(=|\()([\"|']?)([^\"'>|\)]+.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG))/i";
                    preg_match_all($preg, $content, $match);
                    $img_data = $match[4];
                    if ($img_data) {
                        $image = current($img_data);
                    }
                }

                $summary = strip_tags($content);

                if (mb_strlen($summary, 'utf-8') > 60) {
                    $summary = mb_substr($summary, 0, 60, 'utf-8') . '...';
                }
                $form->model()->summary = $summary;
                $form->model()->image = $image;
            });
        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->header)
            ->description('Detail')
            ->body($this->detail($id));
    }

    protected function detail($id)
    {
        return Admin::show(Information::findOrFail($id), function (Show $show) {

            $show->title('标题');
            $show->from('来源');
            $show->content('内容');
            $show->created_at('创建时间');
            $show->updated_at('更新时间');

        });

    }

}
